package com.eight.travel.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.eight.travel.dao.PlaceOrderMapper;
import com.eight.travel.pojo.PlaceOrder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component
public class AliPay {

    public String toPay (PlaceOrder order){

        AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do" ,
                "2021000121698735",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCOv6ShSAr0oh/TEQIVQ7mEQ48vW70bWo7klJVOt+7mMmzIicHjNVA3+6sCjfOhC0CM3sg4iTwoj35VwDDO43nWxtC0dT/ZYi9cCu/9X584XLW0GzT1s09Fu17McZuZ8L3B9GhamVatZsofekK8s5zRomCYp88NmAbcci4uh59p7uwHDmj10Qbcxc21jYbOGeaCBEbb3Zumdjd32GL740yWp4ei5uey2z6qaCw/uYmvxViTrQ8vIWPhMP1jOZjKnxsf9I2dZozqSyTJP5h5jA60uJ7SWTk0NzNO5rwaQkAkrxsuSJ3eIEnXeCpHlWtwv0gLtAtrav3bEIKjFD/dUyrbAgMBAAECggEAWheVvbyMPY087KclW2pc4Z8J9We+2E61NIqaGQ55FovooEBhQBbbxBlv+rLAenlxifbpAtWKREccAt7Dby+eK/Fasyn03CvdnAfDI3SANsA8ZaluKUu70lTJJdrly6hm+sxYOCXi0Jm5I1i0Wgg6BwymTnJKeVfjrKnbf75U3DPbA8RxGT3wQAaCuTOwXUu9Zz2456xQ/XBfFgH5M95UkpCHze/ENM/y5hyHBp5RdOKLcBr99Ty6uNh+E/9enVNs0dt1psRdlnIzJ+5vVBmYjv5vfsKsXnvRtOu/AHYC6UbgLLgUbl4xyz7dPt6PKGTNvB06jIjaELcX79R5vRLBkQKBgQDmOLKqbvhqMbQapYNWPRxYyi02rDJF//gL0sOo/ApD6uTvaXOzKf16uUsaT/WQnA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kahs9wRwvf3DvrCXbb46UwJRdpNOqCvfA+hp6umqnTYz+ZX6i43clLpBNg7+ZUg+1LBVz4OhmVis0w2OTf+azjkPAYYEx1aVudot5SBP0oO0p0Cf5SniYrS03pPBnMH64TtB5k3Zv/9pi0Lz8LwIDAQAB","RSA2");  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
        alipayRequest.setReturnUrl( "http://7687jh.natappfree.cc/place/orderreturn" );
        alipayRequest.setNotifyUrl( "http://7687jh.natappfree.cc/place/orderreturn/"+order.getOid() ); //在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent( "{"  +
                "    \"out_trade_no\":"+order.getOnum()+","  +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
                "    \"total_amount\":"+order.getMoney()+","  +
                "    \"subject\":\"门票\","  +
                "    \"body\":\"门票\","  +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
                "    \"extend_params\":{"  +
                "    \"sys_service_provider_id\":\"2088511833207846\""  +
                "    }" +
                "  }" ); //填充业务参数
        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }
}
